package Core;
/*
 *  J2Egn version 2.6.28
 *  Author: Van Tan Tai 2010-2011
 *  Scroll - Map | List - - -
 */
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class ScrollMe {
	
	private int GAP_MOVE = 50; // pixel
	private int SPEED_FA = 3; // times
	private int SPEED_Fx = 1<<SPEED_FA;
	
	public int HLOADING = 30;
	public int viewW	=0;
	public int viewH	=0;
	/** scrolling content as a list items */
	public boolean isAsList=false;
	/** is Enable moving. false [static] */
	public boolean isActive=false;
	/** is loading at the end of list*/
	public boolean isLoading=false;
	
	private int cmx		=0; // viewX runtime
	private int cmy		=0; // viewY runtime
	private int viewX	=0;
	private int viewY	=0;
	private int x		=0;
	private int y		=0;
	private int w		=0;
	private int h		=0;
	private int xstart	=0;
	private int ystart	=0;
	private Event e			=null;
	private Image currImg	=null;
	private boolean isFixHor=false;
	private boolean isFixVer=false;
	
	public ScrollMe(Image img, int speed) {
		this(img.getWidth(), img.getHeight(), speed);
		currImg = img;
	}
	public ScrollMe(int W_content, int H_content, int speed) {
		viewW = W_content;
		viewH = H_content;
		SPEED_FA = (speed > 0 ? speed : 3);
		SPEED_Fx = 1<<SPEED_FA;
	}
	public void setList(Event endOfList){
		isAsList = true;
		e = endOfList;
	}
	public void setViewer(int left, int top, int width, int height){
		if(width <= 0) width = w;
		if(height <= 0) height = h;
		if(viewW <= width)
			viewX = cmx = (width - viewW) >> 1;
		else if(w != 0){
			w = width;
			fixX(0);//fixX(x-left);
		}
		if (viewH <= height)
			viewY = cmy = (isAsList ? 0 : (height - viewH) >> 1);
		else if(h != 0){
			h = height;
			fixY(0);//fixY(y-top);
		}
		isFixHor = viewW <= width;
		isFixVer = !isAsList && viewH <= height;
		x = left;
		y = top;
		w = width;
		h = height;
	}
	
	/** come to X, Y in frame */
	public void getTo(int x, int y){
		_goTo(x, y);
		cmx = viewX;
		cmy = viewY;
	}
	/** x, y (nguyen duong) la toa do tren tam hinh, nhin thay vung nao trong hinh. */
	public void goTo(int x, int y){
		x = x - (w >> 1); if (x <= 0) x = 0; else x = - x;
		y = y - (h >> 1); if (y <= 0) y = 0; else y = - y;
		_goTo(x, y);
	}
	private void _goTo(int x, int y){
		if(!isFixHor) fixX(-viewX + x); else viewX = (w - viewW) >> 1;// viewW > w
		if(!isFixVer) fixY(-viewY + y); else viewY = (isAsList ? 0 : (h - viewH) >> 1); // viewH > h
	}
	/** Y: yFrame*/
	public void select(int Y, int H){
		if(viewH > h){
		int heiU = - viewY - Y; if(heiU > 0) viewY += heiU; else
		{int heiD = Y + H - (h - viewY); if(heiD > 0) viewY -= heiD;}}
		if(!isLoading && e!= null && Y + H >= viewH) e.action();
	}
	public void scrollLeft(){
		if(isFixHor) return;
		fixX(GAP_MOVE);
	}
	public void scrollRight(){
		if(isFixHor) return;
		fixX(-GAP_MOVE);
	}
	public void scrollUp(){
		if(isFixVer) return;
		fixY(GAP_MOVE);
	}
	public void scrollDown(){
		if(isFixVer) return;
		fixY(-GAP_MOVE);
	}
	public void update(){
		if(isActive && Touch.isPressed)
			return;
		if(!isFixHor){
			if(cmx < viewX) cmx+= (viewX-cmx+SPEED_Fx)>>SPEED_FA;
			else if(cmx > viewX) cmx-= (cmx-viewX+SPEED_Fx)>>SPEED_FA;
		}
		
		if(!isFixVer){
			//System.out.println(cmy +  "   " + viewY);
			if(cmy < viewY) cmy+= (viewY-cmy+SPEED_Fx)>>SPEED_FA;
			else if(cmy > viewY) cmy-= (cmy-viewY+SPEED_Fx)>>SPEED_FA;
		}
	}
	
	public int getX(){return cmx + x;}
	/** y body in real ordinate*/
	public int getY(){return cmy + y;}
	/** y body in Real ordinate*/
	public void setY(int yR){viewY = yR - y; fixY(0); cmy = viewY;}
	/** y real ordinate --> y frame */
	public int gotYin(int yReal){return yReal - (cmy + y);}
	public int getTop(){return y;}
	
	public void destroy(){
		e = null;
		currImg = null;
	}
	
	public void moveStart(int x, int y){
		isActive = (isFixHor && isFixVer) || (x >= this.x && x <= this.x+w && y >= this.y && y <= this.y+h);
		if(!isActive) return;
		
		
		xstart = x;
		ystart = y;
		
		System.out.println("enable aslist " + viewH);
		if(isAsList && (cmy > 0 || cmy < -(viewH - h)))
			return;
		
		
		viewX = cmx;
		viewY = cmy;
	}
	public void moveFinish(int x, int y, int dragTime, int xBegin, int yBegin) {
		if(!isActive) 
		{
			//System.out.println("-----------Drag="+dragTime + " dx="+dx + " dy="+dy);
			return;
		}
		int dx = (x - xBegin);
		int dy = (y - yBegin);
		if(dragTime > 0 && dragTime < 250 && (Math.abs(dx) > 1  || Math.abs(dy) > 1))
		{
			//System.out.println("-----------Drag="+dragTime + " dx="+dx + " dy="+dy);
			if(!isFixHor) fixX((x - xBegin)*(500/dragTime));
			if(!isFixVer) fixY((y - yBegin)*(500/dragTime));
		}
	}
	public void move(int x, int y){
		if(!isActive) return;
		if(!isFixHor){
			fixX(x - xstart);
			cmx = viewX;
		}
		if(!isFixVer){
			if(isAsList){
				cmy += (y - ystart);
				fixY(cmy - viewY);
			} else {
				fixY(y - ystart);
				cmy = viewY;
			}
		}
		xstart = x;
		ystart = y;
	}
	
	private void fixY(int value){
		int yy = viewY + value;
		if(yy > 0) viewY = 0; else
		if(yy < -(viewH - h)){
			viewY = -(viewH - h);
			//List
			if(isAsList){
				if(viewY > 0) viewY = 0;
				if(!isLoading && e!= null && viewH>0) e.action();
			}
		}
		else viewY = yy;
	}
	private void fixX(int value){
		int xx = viewX + value;
		if(xx > 0 ) xx = 0; else
		if(xx < -(viewW - w)) xx = -(viewW - w);
		viewX = xx;
	}

	public boolean isInner(int yReal, int hTest){
		if(yReal + hTest <= y || yReal >= y + h)
			return false;
		return true;
	}
	
	public void showLoading(){
		isLoading = true;
		if(viewH > h && viewY <= -(viewH - h)) viewY -= HLOADING;
		viewH += HLOADING; 
	}
	public void hideLoading(){
		isLoading = false;
		if(viewH > h && viewY <= -(viewH - h)) viewY += HLOADING;
		viewH -= HLOADING; 
	}
	
	public void setClip(Graphics g){
		g.setClip(x, y, w, h);
	}
	public void setClip(Graphics g, int xf, int yf, int wf, int hf){
		int xnew = x < xf ? xf : x;
		int ynew = y < yf ? yf : y;
		g.setClip(xnew, ynew, x + w < xf + wf ? x + w - xf : xf + wf - xnew, y + h < yf + hf ? y + h - yf : yf + hf - ynew);
	}
	
	public void drawMap(Graphics g){
		g.setClip(x, y, w, h);
		g.drawImage(currImg, cmx + x, cmy + y, Graphics.LEFT|Graphics.TOP);
	}
	
	public void drawDebug(Graphics g){
		g.setColor(0xFF0000);
		g.drawRect(x, y, w-1, h-1);
		g.setColor(0x0000FF);
		g.drawRect(getX(), getY(), viewW-1, viewH-1);
	}
}
